package cn.edu.xmu.oomall.customer.dao;

import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.oomall.customer.mapper.CustomerPoMapper;
import cn.edu.xmu.oomall.customer.model.po.CustomerPo;
import cn.edu.xmu.oomall.customer.model.po.CustomerPoExample;
import cn.edu.xmu.privilegegateway.annotation.util.RedisUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
/**
 * @author Jiazhe Yuan
 * @date 2021-12-14 12:16:05
 */
@Repository
public class CustomerDao {
    @Autowired(required = false)
    private CustomerPoMapper customerPoMapper;

    @Autowired
    private RedisUtil redisUtil;

    public PageInfo<CustomerPo> getAllCustomers(String username, String email, String mobile) {
        CustomerPoExample example = new CustomerPoExample();
        CustomerPoExample.Criteria criteria = example.createCriteria();
        criteria.andCreatorIdEqualTo(1L);
        //System.out.println("——————here in dao 1————");
        if (null != username) {
            criteria.andUserNameEqualTo(username);
        }
        if (null != email) {
            criteria.andEmailEqualTo(email);
        }
        if (null != mobile) {
            criteria.andMobileEqualTo(mobile);
        }
        //System.out.println("——————here in dao 2————");

        List<CustomerPo> customerPos = customerPoMapper.selectByExample(example);

        return new PageInfo<>(customerPos);
    }

    public ReturnObject getCustomerById(Long id) {
        CustomerPoExample example = new CustomerPoExample();
        CustomerPoExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);

        CustomerPo customerPo = customerPoMapper.selectByPrimaryKey(id);
        return new ReturnObject(customerPo);
    }
}
